var dictKeyArray = [];
layui.use(['element','table','form','layer','laydate'], function(){
    var table = layui.table,
    form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate,
    layer = layui.layer;

    //执行一个laydate实例
    var beginDateInit = function(startId,endId ){
        laydate.render({
            elem: startId,
            type: 'datetime',
            done: function (value ) {
                var startDate = new Date(value).getTime();
                var endTime = new Date($(endId).val()).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $(startId).val('');
                }
            }
        })
    }
    var endDateInit = function(startId,endId ){
        laydate.render({
            elem: endId,
            type: 'datetime',
            done: function (value ) {
                var startDate = new Date($(startId).val()).getTime();
                var endTime = new Date(value).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $(endId).val('');
                }
            }
        })
    }

    // 开门记录时间
    beginDateInit('#openBeginDate','#openEndDate');
    endDateInit('#openBeginDate','#openEndDate');

    //加载字典项 所属班组，工种
    loadDictList("cdfaceacsTeam","teamIdQuery");
    loadDictList("cdfaceacsWorkType","workTypeIdQuery");

    function loadDictList(dictType,eleId) {
        $.get('/faceAcsWorker/getDictItems', {dictType:dictType}, function (result) {
            if(result != null){
                if(result.data != null){
                    var a$opt_html = "<option value=''>全部</option>";
                    $.each(result.data, function (index, item) {
                        a$opt_html += "<option value='" + item.dictKey + "'>" + item.dictName + "</option>";
                    });
                    $("#"+eleId).empty().append(a$opt_html);
                    form.render('select');
                }
            }
        });
    }

    table.render({
        elem: '#acsRecordTable'
        ,url:'/faceAcsOpenRecord/list'
        ,title: '出入记录'
        ,where: {type:'',name:''}
        ,cols: [[
            {field: 'xh',type:'numbers', title: '序号',}
            ,{field:'personCode', title:'人员编号',align:'center'}
            ,{field:'openUser', title:'人员姓名',align:'center'}
            ,{field:'recordType', title:'记录类型',align:'center'}
            ,{field:'team', title:'班组',align:'center'}
            ,{field:'workType', title:'工种',align:'center'}
            ,{field:'openTime', title:'进出时间',  sort: true,templet:
                    function(data){
                        return formatTime(data.openTime);
                }}
            ,{field:'deviceName', title:'设备别名',align:'center'}
            ,{field:'deviceArea', title:'进出方向',align:'center'}
            ,{field:'capturePic', title:'人脸图片',align:'center',templet:function (d) {
                    if (d.capturePic) {
                        return '<img onclick="magnifyPic(this)" style="cursor: pointer" width="30" height="25" src="' + d.capturePic + '"/>';
                    }
                    return "";
                }}
            ,{field:'deviceIp', title:'设备IP',align:'center'}
        ]]
        ,page: true
    });

    function formatTime(data){
        return data ? data.replace("T", " "):"";
    }

    form.on('submit(query_record)',function () {
        var name = $.trim($("#name").val());
        var teamId = $("#teamIdQuery").val();
        var workTypeId = $("#workTypeIdQuery").val();
        var startDate= $("#openBeginDate").val();
        var endDate= $("#openEndDate").val();
        table.reload('acsRecordTable',{
            where: {name:name,teamId:teamId,workTypeId:workTypeId,startDate:startDate,endDate:endDate}
            ,page:  {
                curr:1
            }
        });
        return false;
    });

    // $("#syncRecord").on("click", function (){
    //     layer.open({
    //         type: 1,
    //         title:"同步开门记录",
    //         area: ['600px', '260px'],
    //         content: $('#updateOpenRecord')
    //     });
    // });

});

function reload(){
    location.reload();
}

function magnifyPic(ele) {
    var width = $(ele).width();
    var height = $(ele).height();
    var scaleWH = width/height;
    var bigH = 450;
    var bigW = scaleWH*bigH;
    if(bigW>600){
        bigW = 600;
        bigH = bigW/scaleWH;
    }
    // 放大预览图片
    layer.open({
        type: 1,
        title: false,
        closeBtn: 1,
        shadeClose: true,
        area: [bigW + 'px', bigH + 'px'], //宽高
        content: "<img width='"+bigW+"' height='"+bigH+"' src=" + $(ele).attr("src") + " />"
    });
}